package com.cheyunkeji.er.activity.evaluate;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.app.PushMsgManager;
import com.cheyunkeji.er.app.UserInfoManager;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.PushMsgBean;
import com.cheyunkeji.er.fragment.evaluate.ArchiveFragment;
import com.cheyunkeji.er.fragment.evaluate.CarFragment;
import com.cheyunkeji.er.fragment.evaluate.EvaluateMineFragment;
import com.cheyunkeji.er.utils.ChannelInfoUtil;
import com.cheyunkeji.er.view.MyFragmentTabHost;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.fragment.app.Fragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class EvaluateHomePageActivity extends BaseActivity {

    private MyFragmentTabHost myFrg;
    private LayoutInflater mInflater;
    private String[] title;
//    private int[] imgIds = {R.drawable.bottom_evaluate_car_selector, R.drawable.bottom_evaluate_selector, R.drawable.bottom_archive_selector, R.drawable.bottom_evaluate_mine_selector};
//    private Class[] fragSet = {CarFragment.class, EvaluateFragment.class, ArchiveFragment.class, EvaluateMineFragment.class};
//    private String[] fragTags = {"car", "evaluate", "archive", "mine"};

    private int[] imgIds = {R.drawable.er_bottom_evaluate_selector, R.drawable.er_bottom_archive_selector, R.drawable.er_bottom_evaluate_mine_selector};
    private Class[] fragSet = {CarFragment.class, ArchiveFragment.class, EvaluateMineFragment.class};
    private String[] fragTags = {"evaluate", "archive", "mine"};

    private Badge mineBadge ;

    @BindView(R.id.iv_close)
    ImageView iv_close;

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_evaluate_home_page);
        EventBus.getDefault().register(this);

        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        mInflater = LayoutInflater.from(this);
        title = getResources().getStringArray(R.array.evaluate_bottom_tab_title);
        myFrg = findViewById(R.id.tabhost);
        myFrg.setup(EvaluateHomePageActivity.this, getSupportFragmentManager(), R.id.actual_content);
        mineBadge = new QBadgeView(this);

        for (int i = 0; i < fragSet.length; i++) {
            View specView = mInflater.inflate(R.layout.er_indicator, null);
            ImageView ivHome = (ImageView) specView.findViewById(R.id.spec_iv);
            TextView tvHome = (TextView) specView.findViewById(R.id.spec_tv);
            RelativeLayout rlImage = (RelativeLayout) specView.findViewById(R.id.rl_iv);
            tvHome.setText(title[i]);
//            tvHome.setTextColor(getResources().getColorStateList(R.color.evaluate_indicator_text_selector));
            ivHome.setBackgroundResource(imgIds[i]);

            TabHost.TabSpec spec1 = myFrg.newTabSpec(fragTags[i]);
            spec1.setIndicator(specView);

            if (i == 3) {
                mineBadge.bindTarget(rlImage).setBadgeGravity(Gravity.TOP | Gravity.END).setShowShadow(false);
            }
            myFrg.addTab(spec1, fragSet[i], null);
        }

//      设置取消显示tab之间的分隔线
        myFrg.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
//      设置默认的选中项为第一个
        myFrg.setCurrentTab(0);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

    }

    public void setCurrentTab(int index ){
        if(null != myFrg && index >= 0 && index < 3){
            myFrg.setCurrentTab(index);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPushMsgReceived(PushMsgBean msgBean) {
        if (msgBean.getNotice_type() == PushMsgManager.MSG_TYPE_ZHANNEIXIN) {
            refreshBottomUnreadMsgCount();
            if(myFrg.getCurrentTab() == 2) {
                ((EvaluateMineFragment) (getSupportFragmentManager().findFragmentByTag(fragTags[3]))).dealWithPushMsg(msgBean);
            }
            EventBus.getDefault().removeStickyEvent(msgBean);
        }
    }


//  获取页面的Fragment
    public Fragment getFragment(int index) {
        return getSupportFragmentManager().findFragmentByTag(fragTags[index]);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshBottomUnreadMsgCount();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void refreshBottomUnreadMsgCount(){
        PushMsgManager pushMsgManager = PushMsgManager.getInstance(MyApplication.getInstance(), ChannelInfoUtil.getCurrentChannelInfo().getChannel() + UserInfoManager.getInstance(this).getAccount());
        int msgCount = pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_ZHANNEIXIN) + pushMsgManager.getMsgCount(PushMsgManager.MSG_TYPE_QUANMIAN) ;
        if (msgCount > 0) {
//          不显示消息记录条数，只显示圆点
            mineBadge.setBadgeNumber(-1);
        } else {
            mineBadge.setBadgeNumber(0);
        }
    }

}
